<template>
    <div>
        <el-table :data="$store.getters['admin/getAdmin']" border style="width: 100%">
            <el-table-column prop="uid" label="角色编号" width="180">
            </el-table-column>
            <el-table-column prop="username" label="用户名称" width="180">
            </el-table-column>
            <el-table-column prop="rolename" label="所属角色">
            </el-table-column>
            <el-table-column label="状态">
                <template slot-scope="item">
                    <div>
                        {{ item.row.status == 1 ? '启用' : '禁用' }}
                    </div>
                </template>
            </el-table-column>
            <el-table-column label="操作">
                <template slot-scope="item">
                    <div>
                        <el-button type="primary" size="mini" icon="el-icon-edit" @click="edit(item.row.uid)">编辑</el-button>
                        <el-button type="danger" icon="el-icon-delete" circle size="mini"
                            @click="del(item.row.uid)"></el-button>
                    </div>
                </template>
            </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-pagination background layout="prev, pager, next" :total="count" 
        :page-size="pageSize" @current-change="changePage">
        </el-pagination>
    </div>
</template>
<script>
import { adminDelete } from '../../request/api'
import { mapGetters } from 'vuex'
export default {
    data() {
        return {

        }
    },
    methods: {
        // 切换页码并获取当前页码事件
        changePage(n){
            this.$store.dispatch('admin/changePageAction',n)
        },
        // 编辑事件
        edit(uid) {
            this.$emit('edit', uid)
        },
        // 删除事件
        del(uid) {
            this.$confirm('你确定要删除吗', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消'
            }).then(() => {
                adminDelete({ uid }).then(res => {
                    if (res.code == 200) {
                        this.$message.success(res.msg)
                        // 重新调用总列表行动
                        this.$store.dispatch('admin/getAdminListAction')
                        // 重新调用总条数行动
                        this.$store.dispatch('admin/getAdminCount')
                    } else {
                        this.$message.error(res.msg)
                    }
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                })
            })
        }
    },
    mounted() {
        // 组件一触发就加载列表行动
        this.$store.dispatch('admin/getAdminListAction')
        this.$store.dispatch('admin/getAdminCount')
    },
    watch: {},
    computed: {
        ...mapGetters({
            adminList:'admin/getAdmin',
            pageSize:'admin/getSize',
            count:'admin/getCount'
        })
    },
    filters: {},
    components: {}
}
</script>
<style scoped></style>